/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ws.restful;

import com.beans.controlPrincipal;
import com.entidades.Cupones;
import com.entidades.Datoscuponesventa;
import com.entidades.Ofertas;
import com.entidades.Usuarios;
import com.entidades.Ventas;
import com.entidades.WsOfertasfull;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.el.ELException;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

/**
 *
 * @author JAVIER
 */
@Stateless
@Path("com.entidades.wsofertasfull")
public class WsOfertasfullFacadeREST extends AbstractFacade<WsOfertasfull> {
    @PersistenceContext(unitName = "AgrupadosSAPU")
    private EntityManager em;

    public WsOfertasfullFacadeREST() {
        super(WsOfertasfull.class);
    }

    @POST
    @Override
    @Consumes({"application/xml", "application/json"})
    public void create(WsOfertasfull entity) {
        super.create(entity);
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(WsOfertasfull entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public WsOfertasfull find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<WsOfertasfull> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<WsOfertasfull> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }
  
    @GET
    @Path("{PorderBy}/{PmaxPrecio}/{Ptipo}/{Pdireccion}/{Pnegocio}")
    @Produces({"application/xml", "application/json"})
    public List<WsOfertasfull> findOfertasFull(
            @PathParam("PorderBy") String PorderBy, 
            @PathParam("PmaxPrecio") Integer PmaxPrecio,
            @PathParam("Ptipo") String Ptipo,
            @PathParam("Pdireccion") String Pdireccion,
            @PathParam("Pnegocio") String Pnegocio
     ){
        if(PorderBy == null || PorderBy.isEmpty()){
                PorderBy = "id_oferta";
                Ptipo = "";
                Pdireccion = "";
                PmaxPrecio = 1000;
                Pnegocio = "%";
            }
        if(Pdireccion.equals("all")){Pdireccion = "";}
        if(Pnegocio.equals("all")){Pnegocio = "%";}
        if(Ptipo.equals("all")){Ptipo = "";}
        
        Pdireccion = Pdireccion.replaceAll(Pattern.quote("+"), " ");
        Pnegocio = Pnegocio.replaceAll(Pattern.quote("+"), " ");
        Ptipo = Ptipo.replaceAll(Pattern.quote("+"), " ");
        PorderBy = PorderBy.replaceAll(Pattern.quote("+"), " ");
        
            String SQLstring;
            SQLstring = "select * from bdagrupados.ws_ofertasfull "
                       +" where ofe_precio <= " + PmaxPrecio
                       +" and   ofe_tipo like '%" + Ptipo + "%'"
                       +" and   neg_direccion like '%" + Pdireccion + "'"
                       +" and   neg_nombre like '" + Pnegocio + "'"
                       +" order by " + PorderBy;
            try {
                List<WsOfertasfull> tmpOfertasFull = (List<WsOfertasfull>) em.createNativeQuery(SQLstring, WsOfertasfull.class).getResultList();
                return tmpOfertasFull;
                
            } catch (NoResultException nre) {
                return null;
            }
        
    }
    
    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
